﻿#region

using System;
using System.Drawing;
using Tank90.Properties;

#endregion

namespace Tank90
{
    public class Enemy : Tank
    {
        private readonly PlayerTank player;
        private readonly Random random = new Random();
        private int _EnemyType;

        public Enemy(PlayerTank player)
        {
            this.player = player;
            IsFire = true;
            IsMove = true;
            BornTime = 0;
            y = 0;
        }

        public bool IsMove { get; set; }
        public static int AdjustDirection { get; set; }
        public static int FireRandom { get; set; }
        public static int AdjustRandom { get; set; }
        public bool IsFiring { get; set; }
        public bool IsRed { get; set; }
        public bool IsFire { get; set; }

        public int EnemyType
        {
            get { return _EnemyType; }
            set
            {
                _EnemyType = value;
                SetProprietyByType(value);
            }
        }

        #region

        private readonly Image[][] RedImages =
        {
            new Image[] //此处有问题有空值应该是修改名称的问题
            {
                Resources.RedEnemy1Up,
                Resources.RedEnemy1Down,
                Resources.RedEnemy1Left,
                Resources.RedEnemy1Right
            },
            new Image[]
            {
                Resources.RedEnemy2Up,
                Resources.RedEnemy2Down,
                Resources.RedEnemy2Left,
                Resources.RedEnemy2Right
            },
            new Image[]
            {
                Resources.RedEnemy3Up,
                Resources.RedEnemy3Down,
                Resources.RedEnemy3Left,
                Resources.RedEnemy3Right
            }
        };

        private readonly Image[][] enemyImages =
        {
            new Image[]
            {
                Resources.Enemy1Up,
                Resources.Enemy1Down,
                Resources.Enemy1Left,
                Resources.Enemy1Right
            },
            new Image[]
            {
                Resources.Enemy2Up,
                Resources.Enemy2Down,
                Resources.Enemy2Left,
                Resources.Enemy2Right
            },
            new Image[]
            {
                Resources.FEnemy3Up,
                Resources.FEnemy3Down,
                Resources.FEnemy3Left,
                Resources.FEnemy3Right
            },
            new Image[]
            {
                Resources.SEnemy3Up,
                Resources.SEnemy3Down,
                Resources.SEnemy3Left,
                Resources.SEnemy3Right
            },
            new Image[]
            {
                Resources.TEnemy3Up,
                Resources.TEnemy3Down,
                Resources.TEnemy3Left,
                Resources.TEnemy3Right
            }
        };

        private Image[] EnemyCommon =
        {
            Resources.Enemy1Up,
            Resources.Enemy1Down,
            Resources.Enemy1Left,
            Resources.Enemy1Right
        };


        private Image[] EnemySlow =
        {
            Resources.FEnemy3Up,
            Resources.FEnemy3Down,
            Resources.FEnemy3Left,
            Resources.FEnemy3Right
        };

        private Image[] EnemySpeed =
        {
            Resources.Enemy2Up,
            Resources.Enemy2Down,
            Resources.Enemy2Left,
            Resources.Enemy2Right
        };

        #endregion

        public void GetDirection()
        {
            if (random.Next(1, 10) < 5)
            {
                if (x > player.x)
                {
                    directions = Directions.Left;
                }
                if (x < player.x)
                {
                    directions = Directions.Right;
                }
            }
            else
            {
                //if (random.Next(1, 10) < 5)
                //{

                if (y > player.y)
                {
                    directions = Directions.Up;
                }
                if (y < player.y)
                {
                    directions = Directions.Down;
                }

                //}    SetDirection();
            }
            SetDirection();
        }

        public void AdjustDirections()
        {
            directions = (Directions) random.Next(0, 4);
            SetDirection();
        }

        public void Move()
        {
            if (!died && !IsBorned)
            {
                staticX = x;
                staticY = y;
                IsBeyond();
                SetDirection();
            }
        }

        private void SetDirection()
        {
            if (!IsRed)
            {
                #region 正常的坦克

                if (directions == Directions.Up)
                {
                    if (EnemyType != 3)
                    {
                        dirImage = enemyImages[EnemyType - 1][0];
                    }
                    else if (EnemyType == 3 && life == 3)
                    {
                        dirImage = enemyImages[EnemyType - 1][0];
                    }
                    if (EnemyType == 3 && life == 2)
                    {
                        dirImage = enemyImages[EnemyType][0];
                    }
                    else if (EnemyType == 3 && life == 1)
                    {
                        dirImage = enemyImages[EnemyType + 1][0];
                    }

                    y -= speed;
                    BulletX = x + 22;
                    BulletY = y + 15;
                }
                if (directions == Directions.Down)
                {
                    if (EnemyType != 3)
                    {
                        dirImage = enemyImages[EnemyType - 1][1];
                    }
                    else if (EnemyType == 3 && life == 3)
                    {
                        dirImage = enemyImages[EnemyType - 1][1];
                    }
                    if (EnemyType == 3 && life == 2)
                    {
                        dirImage = enemyImages[EnemyType][1];
                    }
                    else if (EnemyType == 3 && life == 1)
                    {
                        dirImage = enemyImages[EnemyType + 1][1];
                    }

                    y += speed;
                    BulletX = x + 22;
                    BulletY = y + 25;
                }
                if (directions == Directions.Left)
                {
                    if (EnemyType != 3)
                    {
                        dirImage = enemyImages[EnemyType - 1][2];
                    }
                    else if (EnemyType == 3 && life == 3)
                    {
                        dirImage = enemyImages[EnemyType - 1][2];
                    }
                    if (EnemyType == 3 && life == 2)
                    {
                        dirImage = enemyImages[EnemyType][2];
                    }
                    else if (EnemyType == 3 && life == 1)
                    {
                        dirImage = enemyImages[EnemyType + 1][2];
                    }
                    x -= speed;
                    BulletX = x + 15;
                    BulletY = y + 22;
                }
                if (directions == Directions.Right)
                {
                    if (EnemyType != 3)
                    {
                        dirImage = enemyImages[EnemyType - 1][3];
                    }
                    else if (EnemyType == 3 && life == 3)
                    {
                        dirImage = enemyImages[EnemyType - 1][3];
                    }
                    if (EnemyType == 3 && life == 2)
                    {
                        dirImage = enemyImages[EnemyType][3];
                    }
                    else if (EnemyType == 3 && life == 1)
                    {
                        dirImage = enemyImages[EnemyType + 1][3];
                    }
                    x += speed;
                    BulletX = x + 20;
                    BulletY = y + 22;
                }

                #endregion
            }
            else
            {
                #region 红色坦克

                if (directions == Directions.Up)
                {
                    dirImage = RedImages[EnemyType - 1][0];
                    if (EnemyType == 3 && life == 2)
                    {
                        dirImage = enemyImages[EnemyType][0];
                    }
                    else if (EnemyType == 3 && life == 1)
                    {
                        dirImage = enemyImages[EnemyType + 1][0];
                    }

                    y -= speed;
                    BulletX = x + 22;
                    BulletY = y + 15;
                }
                if (directions == Directions.Down)
                {
                    dirImage = RedImages[EnemyType - 1][1];
                    if (EnemyType == 3 && life == 2)
                    {
                        dirImage = enemyImages[EnemyType][1];
                    }
                    else if (EnemyType == 3 && life == 1)
                    {
                        dirImage = enemyImages[EnemyType + 1][1];
                    }
                    y += speed;
                    BulletX = x + 22;
                    BulletY = y + 25;
                }
                if (directions == Directions.Left)
                {
                    dirImage = RedImages[EnemyType - 1][2];
                    if (EnemyType == 3 && life == 2)
                    {
                        dirImage = enemyImages[EnemyType][2];
                    }
                    else if (EnemyType == 3 && life == 1)
                    {
                        dirImage = enemyImages[EnemyType + 1][2];
                    }
                    x -= speed;
                    BulletX = x + 15;
                    BulletY = y + 22;
                }
                if (directions == Directions.Right)
                {
                    dirImage = RedImages[EnemyType - 1][3];
                    if (EnemyType == 3 && life == 2)
                    {
                        dirImage = enemyImages[EnemyType][3];
                    }
                    else if (EnemyType == 3 && life == 1)
                    {
                        dirImage = enemyImages[EnemyType + 1][3];
                    }
                    x += speed;
                    BulletX = x + 20;
                    BulletY = y + 22;
                }

                #endregion
            }
        }

        public override void Drawing(Graphics graphics)
        {
           if (BornTime<48)
            {
                BornTime++;
                return;
            }
           else
           {
                if (IsDied)
                {
                    return;
                }
                if (IsMove)
                {
                    if (random.Next(1, 3) == 2)
                    {
                        Move();
                    }
                    if (random.Next(1, 20) == 2)
                    {
                        GetDirection();
                    }
                }

                if (dirImage == null) //怎么会有空值呢？？？？？？？？？？？、、
                {
                    if (IsRed)
                    {
                        dirImage = RedImages[EnemyType - 1][1];
                    }
                    else
                    {
                        dirImage = enemyImages[EnemyType - 1][1];
                    }
                    directions = Directions.Down;
                }

                graphics.DrawImage(dirImage, new Point(x, y));
                if (random.Next(1, 50) < 4)
                {
                    if ((!IsFiring || Bullet == null) && IsFire)
                    {
                        SetBullet();
                        Bullet = new Bullet(BulletX, BulletY, directions);

                        IsFiring = true;
                    }
                }
                if (IsFiring && Bullet != null)
                {
                    Bullet.Move();
                    if (!Bullet.IsBeyondEdge())
                    {
                        Bullet.Drawing(graphics);
                    }
                    else
                    {
                        IsFiring = false;
                        Bullet = null;
                    }
                }
            }

        }

        private void SetProprietyByType(int type)
        {
            switch (type)
            {
                case 1:
                    speed = 2;
                    life = 1;
                    break;
                case 2:
                    speed =4;
                    life = 1;
                    break;
                case 3:
                    speed = 1;
                    life = 3;
                    break;
            }
        }

        //private void SetTankImage()
        //{
        //    if (EnemyType==3)
        //    {
        //        if (life==3)
        //        {
        //            dirImage=Resources.te
        //        }
        //    }
        //}
    }
}